/*   Copyright 2012 Fernando Moreno Ruiz All Rights Reserved.
 *
 * 	 This file is part of pyPhotosMixer.
 *
 *   pyPhotosMixer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   pyPhotosMixer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with pyPhotosMixer.  If not, see <http://www.gnu.org/licenses/>.
 * */
import QtQuick 1.1
import Qt.labs.folderlistmodel 1.0
import "../javascript/Date.js" as JsDate
Item {
    property variant modelCamera1:_modelFolderCamera1;
    property variant modelCamera2:_modelFolderCamera2;
    property ListModel modelExifCamera1:  _exifsCamera1;
    property ListModel modelExifCamera2:  _exifsCamera2;
    property bool camera1FerFormatAvailable:false
    property bool camera2FerFormatAvailable:false
    property string folderCamera1: ""
    property string folderCamera2: ""
    property string maxDateCamera1: ""
    property string maxDateCamera2: ""
    property string minDateCamera1: ""
    property string minDateCamera2: ""
    property string maxDateCamera1Fer: ""
    property string maxDateCamera2Fer: ""
    property string minDateCamera1Fer: ""
    property string minDateCamera2Fer: ""
    property string minDateTotal: ""
    property string maxDateTotal: ""

    property bool modelCamera1Loaded: false
    property bool modelCamera2Loaded: false
    signal allModelsLoaded()
    //    property string folderCamera1: "file:/C:/Users/Fernando/Desktop/montes de malaga 2-1-2012/movilfer"
    //    property string folderCamera2: "file:/C:/Users/Fernando/Desktop/montes de malaga 2-1-2012/ianet"
    function allMinDateFilled(){
        return minDateCamera1 != "" && minDateCamera2!= ""
    }
    function allMaxDateFilled(){
        return maxDateCamera1!="" && maxDateCamera2!=""
    }
    function checkMax (max) {
        console.log("_MAX:"+max)
        if(maxDateTotal==""){
            console.log("MAXPPP:"+max);
            maxDateTotal = max
        }else if (JsDate.stringToDate(max) > JsDate.stringToDate(maxDateTotal))
            maxDateTotal = max
    }

    function checkMin (min) {
        if(minDateTotal=="")
            minDateTotal = min
        else if (JsDate.stringToDate(min) < JsDate.stringToDate(minDateTotal))
            minDateTotal = min
    }

    onMaxDateTotalChanged: {
        console.log("MAXDATETOTAL:" + maxDateTotal);
    }

    onMinDateTotalChanged: {
        console.log("MINDATETOTAL:" + minDateTotal);
    }

    onMinDateCamera1FerChanged: {
        checkMin(minDateCamera1Fer)
    }

    onMinDateCamera2FerChanged: {
        checkMin(minDateCamera2Fer)
    }

    onMinDateCamera1Changed: {
        checkMin(minDateCamera1)
    }

    onMinDateCamera2Changed: {
        checkMin(minDateCamera2)
    }

    onMaxDateCamera1FerChanged: {
        checkMax(maxDateCamera1Fer)
    }

    onMaxDateCamera2FerChanged: {
        checkMax(maxDateCamera2Fer)
    }

    onMaxDateCamera1Changed: {
        checkMax(maxDateCamera1)
    }

    onMaxDateCamera2Changed: {
        checkMax(maxDateCamera2)
    }

    onFolderCamera1Changed: {
        _exifsCamera1.clear()
    }
    onFolderCamera2Changed: {
        _exifsCamera2.clear()
    }
    FolderListModel {
        id: _modelFolderCamera1
        folder: folderCamera1
        nameFilters: ["*.jpg"]
        onFolderChanged: {
            _exifsCamera1.clear()
        }
        Component.onCompleted: {
            console.log(count);
        }
    }
    FolderListModel {
        id: _modelFolderCamera2
        folder: folderCamera2
        nameFilters: ["*.jpg"]
        onFolderChanged: {
            _exifsCamera2.clear()
        }
        Component.onCompleted: {
            console.log(count);
        }
        onRowsInserted: {
            //interesante
        }
    }
    GenerateModelCamera{
        id:generateModelCamera1
        model: _modelFolderCamera1
        listToGenerate: _exifsCamera1
        onLoadFinished: {
             console.log("Count lista generada1:"+_exifsCamera1.count)
            minDateCamera1 = minDate
            maxDateCamera1 = maxDate
            console.log("minDateFerFormat1:" + minDateFerFormat)
            console.log("maxDateFerFormat1:" + maxDateFerFormat)
            if(minDateFerFormat!=="" && maxDateFerFormat!==""){
                camera1FerFormatAvailable = true
            }else {
                camera1FerFormatAvailable = false
            }
            minDateCamera1Fer = minDateFerFormat
            maxDateCamera1Fer  = maxDateFerFormat
            if(maxDate!="" && minDate != ""){
                console.log("MAxDate!=" + maxDate + " -MinDate:"+minDate)
                modelCamera1Loaded = true
            }

            if(modelCamera1Loaded && modelCamera2Loaded) {
                allModelsLoaded()
            }
        }
        onListToGenerateChanged: {
            console.log("Count lista generada1:"+_exifsCamera1.count)
        }
    }

    GenerateModelCamera{
        id:generateModelCamera2
        model: _modelFolderCamera2
        listToGenerate: _exifsCamera2
        onLoadFinished: {
            console.log("_modelFolderCamera2")
             console.log("Count lista generada2:"+_exifsCamera2.count)
            minDateCamera2 = minDate
            console.log("minDateCAmera2")
            maxDateCamera2 = maxDate
            console.log("minDateFerFormat2:" + typeof minDateFerFormat)
            console.log("maxDateFerFormat2:" + typeof maxDateFerFormat)
            if(minDateFerFormat!=="" && maxDateFerFormat!==""){
                camera2FerFormatAvailable = true
            }else {
                camera2FerFormatAvailable = false
            }
            minDateCamera2Fer = minDateFerFormat
            maxDateCamera2Fer  = maxDateFerFormat

            if(maxDate!="" && minDate != ""){
            console.log("MAxDate!=" + maxDate + " -MinDate:"+minDate)
                modelCamera2Loaded = true
            }

            if(modelCamera1Loaded && modelCamera2Loaded) {
                allModelsLoaded()
            }
            console.log("minDateCamera2Fer:"+minDateCamera2Fer);
            console.log("maxDateCamera2Fer:"+maxDateCamera2Fer);
        }
        onListToGenerateChanged: {
            console.log("Count lista generada1:"+_exifsCamera2.count)
        }
    }

    ListModel{
        id: _exifsCamera1
    }
    ListModel{
        id: _exifsCamera2
    }

}
